package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"shop-order-pro/cache"
	"shop-order-pro/modler"
	"shop-order-pro/pkg"
	"strconv"
	"time"
)

func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	redis_key := "UserLogin" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var user modler.User
	err := modler.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}

func UserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := modler.UserRole{
		Uid: uid,
		Rid: rid,
	}
	modler.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := modler.RoleNode{

		Rid: rid,
		Nid: nid,
	}
	modler.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func UserList(c *gin.Context) {
	uid := c.Query("uid")
	/*
		    SELECT
			  *
			FROM
			  roles
			  LEFT JOIN user_roles ON roles.id = user_roles.rid
	*/
	var list modler.Role
	modler.DB.Table("roles").
		Joins(" LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid=?", uid).
		First(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func RoleList(c *gin.Context) {
	rid := c.Query("rid")
	/*
		     SELECT
			  *
			FROM
			  nodes
			  LEFT JOIN role_nodes ON nodes.id = role_nodes.nid
			  WHERE role_nodes.rid=1
	*/
	var list modler.Node
	modler.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where(" role_nodes.rid=?", rid).
		First(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func ShopAdd(c *gin.Context) {
	shopCode := c.PostForm("shopCode")
	shopName := c.PostForm("shopName")
	shopNum := c.PostForm("shopNum")
	shopType := c.PostForm("shopType")
	if shopCode == "" || shopName == "" || shopNum == "" || shopType == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := modler.Shop{

		ShopCode: shopCode,
		ShopName: shopName,
		ShopNum:  shopNum,
		ShopType: shopType,
	}
	modler.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ShopOrderAdd(c *gin.Context) {
	shopOrderCode := c.PostForm("shopOrderCode")
	shopOrderName := c.PostForm("shopOrderName")
	shopOrderPrince := c.PostForm("shopOrderPrince")
	shopOrderNO := c.PostForm("shopOrderNO")
	sid, _ := strconv.Atoi(c.PostForm("sid"))
	if shopOrderCode == "" || shopOrderName == "" || shopOrderPrince == "" || shopOrderNO == "" || sid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := modler.ShopOrder{

		ShopOrderCode:   shopOrderCode,
		ShopOrderName:   shopOrderName,
		ShopOrderPrince: shopOrderPrince,
		ShopOrderNO:     shopOrderNO,
		Sid:             sid,
	}
	modler.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ShopList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	var count int64
	modler.DB.Model(&modler.ShopOrder{}).Count(&count)

	/*
		     SELECT
			  shop_orders.id AS OrderId,
			  shops.shop_code AS ShopCode,
			  shops.shop_name AS ShopName,
			  shops.shop_num AS ShopNum,
			  shops.shop_type AS ShopType,
			  shop_orders.shop_order_code AS ShopOrderCode,
			  shop_orders.shop_order_name AS ShopOrderName,
			  shop_orders.shop_order_prince AS ShopOrderPrince,
			  shop_orders.shop_order_no AS ShopOrderNO
			FROM
			  shop_orders
			  LEFT JOIN shops ON shops.id = shop_orders.sid
	*/

	var list []modler.ShopOrderList
	redis_key := "ShopList" + strconv.Itoa(page)
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Table("shop_orders").
			Select(
				"shop_orders.id AS OrderId",
				"shops.shop_code AS ShopCode",
				"shops.shop_name AS ShopName",
				"shops.shop_num AS ShopNum",
				"shops.shop_type AS ShopType",
				"shop_orders.shop_order_code AS ShopOrderCode",
				"shop_orders.shop_order_name AS ShopOrderName",
				"shop_orders.shop_order_prince AS ShopOrderPrince",
				"shop_orders.shop_order_no AS ShopOrderNO",
			).
			Joins("LEFT JOIN shops ON shops.id = shop_orders.sid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return
}

func ShopDetail(c *gin.Context) {
	id := c.Query("id")
	var list modler.ShopOrderList
	redis_key := "ShopDetail" + id
	result, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Table("shop_orders").
			Select(
				"shop_orders.id AS OrderId",
				"shops.shop_code AS ShopCode",
				"shops.shop_name AS ShopName",
				"shops.shop_num AS ShopNum",
				"shops.shop_type AS ShopType",
				"shop_orders.shop_order_code AS ShopOrderCode",
				"shop_orders.shop_order_name AS ShopOrderName",
				"shop_orders.shop_order_prince AS ShopOrderPrince",
				"shop_orders.shop_order_no AS ShopOrderNO",
			).
			Joins("LEFT JOIN shops ON shops.id = shop_orders.sid").
			Where("shop_orders.id=?", id).
			Debug().
			Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
